package com.example.studentmanager.Controller;

import com.example.studentmanager.Pojo.Home;
import com.example.studentmanager.Service.impl.homeimpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "home",value = "/home")
public class HomeController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        homeimpl ho=new homeimpl();
      List<Home> list= ho.Qhome();
      if (list.isEmpty()){
          req.setAttribute("error","宿舍信息有误");
          RequestDispatcher ds= req.getRequestDispatcher("./404.jsp");
          ds.forward(req,res);
      }else {
          HttpSession s=req.getSession();
          s.setAttribute("user","admin");
          s.setAttribute("password","123456");
          req.setAttribute("list",list);
          RequestDispatcher ds= req.getRequestDispatcher("./home.jsp");
          ds.forward(req,res);
      }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        homeimpl ho=new homeimpl();
        List<Home> list= ho.Qhome();
        if (list.isEmpty()){
            req.setAttribute("error","宿舍信息有误");
            RequestDispatcher ds= req.getRequestDispatcher("./404.jsp");
            ds.forward(req,res);
        }else {
            HttpSession s=req.getSession();
            s.setAttribute("user","admin");
            s.setAttribute("password","123456");
            req.setAttribute("list",list);
            RequestDispatcher ds= req.getRequestDispatcher("./home.jsp");
            ds.forward(req,res);
        }
    }
}
